package cn.maxwell.org.service.impl;


import cn.maxwell.basic.service.impl.BaseServiceImpl;
import cn.maxwell.basic.util.BusinessException;
import cn.maxwell.org.domain.Department;
import cn.maxwell.org.mapper.DepartmentMapper;
import cn.maxwell.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：部门
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 添加
     * 部门选择框级联对应数据库dirPath路径展示
     * @param department
     */
    @Transactional
    @Override
    public void add(Department department) {
        //先进行添加操作
        departmentMapper.add(department);
        //获取id
        if(department.getParent_id()==null){
            //获取自己的id作为顶级部门
            Long id = department.getId();
            //做拼接回显操作
            department.setDirPath("/"+id);
        }else{
            //获取上级部门id
            Long parent_id = department.getParent_id();
            //通过上级部门id查询上级部门
            Department parentDepartment = departmentMapper.loadById(parent_id);
            //做拼接回显操作，拼接上级部门+自己的id
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }

    /**
     * 修改
     *
     * @param department
     */
    @Transactional
    @Override
    public void update(Department department) {
        //获取id
        if(department.getParent_id()==null){
            //获取自己的id作为顶级部门
            Long id = department.getId();
            //做拼接回显操作
            department.setDirPath("/"+id);
        }else{
            //获取上级部门id
            Long parent_id = department.getParent_id();
            //通过上级部门id查询上级部门
            Department parentDepartment = departmentMapper.loadById(parent_id);
            //做拼接回显操作，拼接上级部门+自己的id
            department.setDirPath(parentDepartment.getDirPath()+"/"+department.getId());
        }
        departmentMapper.update(department);
    }


    @Override
    public List<Department> deptTree() {
        //查询所有部门
        List<Department> departments = departmentMapper.loadAll();
        //创建一个map
        Map<Long, Department> map = new HashMap<>();
        //遍历查询到的所有部门，将部门ID和部门对象存放到map中
        for(Department department : departments){
            map.put(department.getId(),department);
        }
        //创建一个list,用来存放顶级部门
        List<Department> deptTree = new ArrayList<>();
        for(Department department : departments){
            //如果Parent_id为空的话说明它是顶级部门
            if(department.getParent_id() == null){
                //是顶级部门的话直接添加进部门树
                deptTree.add(department);
            }else{
                //如果Parent_id不为空，获取它的父级部门id
                Long parent_id = department.getParent_id();
                //根据查询到的父级id在map中匹配对应的上级部门department对象
                Department parentDepartment = map.get(parent_id);
                //给上级部门添加子级部门(也就是把自己添加到顶级部门的子级)
                parentDepartment.getChildren().add(department);
            }
        }
        return deptTree;
    }
}
